//
//  DataOperationManager.h
//  etionCBLogic
//
//  Created by wjxfhxy on 13-3-12.
//
//

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSUInteger,EDOMRequestStateType)
{
    EDOMRequestStateTypeReject = 1,
    EDOMRequestStateTypeCanRequest = 2,
    EDOMRequestStateTypeRequesting = 4,
    EDOMRequestStateTypeStop = 8,
    EDOMRequestStateTypeCanNotRequest = (EDOMRequestStateTypeReject | EDOMRequestStateTypeStop),
    EDOMRequestStateTypeWaitingQueueTasks = 16
} ;

typedef NS_ENUM(NSUInteger,EDispatchQueueRunType)
{
    EDispatchQueueRunTypeAsync,
    EDispatchQueueRunTypeSync,
};

@class XMSDataOperationManager;

@protocol XMSDataOperationManagerDelegate <NSObject>

@optional
//- (void)WillTerminateRequest:(XMSDataOperationManager *)dom;
//
//- (void)DidTerminateRequest:(XMSDataOperationManager *)dom;

@end

@interface XMSDataOperationManager : NSObject
{
    id <XMSDataOperationManagerDelegate> _delegate;

    NSString *_name;

    NSMutableArray *_arAttachdom;
}

@property(nonatomic, assign) id <XMSDataOperationManagerDelegate> delegate;
@property(nonatomic, readonly) BOOL isRejectRequest;
@property(nonatomic, readonly) BOOL isStopRequest;
@property(nonatomic, readonly) BOOL isRequesting;
@property(nonatomic, readonly) BOOL isCanRequest;//!(stop & reject)
@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) XMSDispatchQueueWrapper *dispatchQueueWrapper;

- (id)initWithDataOperationManagerDelegate:(id <XMSDataOperationManagerDelegate>)domdelegate;

- (id)initWithDataOperationManagerDelegate:(id <XMSDataOperationManagerDelegate>)domdelegate AttachDOM:(XMSDataOperationManager *)attachdom;

- (id)initWithDispatchQueueWrapper:(XMSDispatchQueueWrapper*)wrapper;

- (BOOL)dataOperationManagerDelegate:(id <XMSDataOperationManagerDelegate>)domdelegate attachDOMCls:(NSArray *)arAttachdom operation:(void (^)(NSArray *arDom))operation;

- (BOOL)attachDataOperationManager:(NSArray *)arDom;

- (void)detachAllDataOperationManager;

- (void)detachDataOperationManager:(NSArray *)arDom;

- (BOOL)isDOMAttachRequesting;

/**
 *  若没有开启线程，此方法会开启线程，执行block
 *
 *  @param block 执行的任务
 *
 *  @return 返回执行状态：EDOMRequestStateTypeCanNotRequest，不能被执行；EDOMRequestStateTypeCanRequest，提交的到线程中等待执行
 */
- (EDOMRequestStateType)requestAsyncBlock:(void (^)())block;

/**
 *  此方法不会开启线程，即是在当前线程中执行block
 *
 *  @param block 执行的任务
 *
 *  @return 返回执行状态：EDOMRequestStateTypeCanNotRequest，不能被执行；EDOMRequestStateTypeCanRequest，提交的到线程中等待执行
 */
- (EDOMRequestStateType)requestSyncBlock:(void (^)())block;

/**
 *  不管调用方法是否在线程中执行，此方法都会开启新的线程并执行block
 *
 *  @param block 执行的任务
 *
 *  @return 返回执行状态：EDOMRequestStateTypeCanNotRequest，不能被执行；EDOMRequestStateTypeCanRequest，提交的到线程中等待执行
 */
- (EDOMRequestStateType)requestThreadBlock:(void (^)())block;

//terminateRequestAsyncComplete->terminateRequest->terminateSelfRequest
/**
 *  异步中断请求
 *
 *  @param complete 操作成功与否
 */
- (void)terminateRequestAsyncCompletion:(void (^)(NSInteger nResult))completion;

/**
 *  异步中断请求
 */
- (void)terminateRequestAsyncCompletion;

//terminateRequest->terminateSelfRequest
/**
 *  阻塞当前线程，中断请求
 *
 *  @return 操作成功与否
 */
- (BOOL)terminateRequest;

/**
 *  对应于terminateRequest，取消中断标记
 *
 *  @return 操作成功与否
 */
- (BOOL)cancelTerminateRequest;

/**
 *  子类方法只能对此类重写，用于中断、停止自身的请求
 *
 *  @param bRejectrequest YES，中断请求；NO，停止请求
 *
 *  @return
 */
- (BOOL)terminateSelfRequest:(BOOL)bRejectrequest;

/**
 *  异步停止请求
 *
 *  @param complete 操作成功与否
 */
- (void)stopRequestAsyncCompletion:(void (^)(NSInteger nResult))completion;

/**
 *  异步停止请求
 */
- (void)stopRequestAsyncCompletion;

/**
 *  阻塞当前线程，停止请求
 *
 *  @return 操作成功与否
 */
- (BOOL)stopRequest;

//- (void)addTerminateBlock:(void(^)(BOOL bRejectrequest))block;

- (void)dispatchQueueRunType:(EDispatchQueueRunType)type checkTerminate:(BOOL)check asyncBlock:(void (^)())block;

@end

@interface XMSDataOperationManager (SingleQueue)

//把任务加到线程中按顺序执行，且不阻塞当前线程
- (EDOMRequestStateType)requestQueueAsyncBlock:(void (^)())block;

- (EDOMRequestStateType)requestQueueSyncBlock:(void (^)())block;

@end

@interface XMSDataOperationManager (QueuePool)

@property(nonatomic, readonly,getter = isIsQueuePoolWaitingFinish) BOOL isQueuePoolWaitingFinish;

- (void)setQueuePoolMaxRun:(NSUInteger)nMax;

//把任务加到线程池(队列)中，等待调度执行
- (EDOMRequestStateType)requestQueuePoolAsyncBlock:(void (^)())block;

- (EDOMRequestStateType)requestInvocationQueuePoolAsyncInvocation:(NSInvocationOperation *)invocation;

- (NSArray *)queuePoolTask;

- (NSUInteger)queuePoolTaskCount;

- (void)waitUntilAllQueuePoolTasksAreFinished;

@end
